//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Windows;
using OpenLS.Drawing.UI;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    public class PropertyHelper
    {
        public static bool ShowPropertiesNonHostedFor(FrameworkElement caller, string title, object obj, params ResourceDictionary[] resources)
        {
            return ShowPropertiesNonHostedForImp(caller, title, obj, resources);
        }

        private static bool ShowPropertiesNonHostedForImp(FrameworkElement caller, string title, object obj, IEnumerable<ResourceDictionary> resources)
        {
            var w = new PropertiesWindow { Owner = WindowServices.GetAncestor<Window>(caller), Title = title };
            foreach (var resource in resources)
            {
                w.Resources.MergedDictionaries.Add(resource);
            }

            w.DataContext = obj;
            TransferProperties(caller, w);
            return w.ShowDialog().GetValueOrDefault();
        }

        public static void TransferProperties(DependencyObject source, DependencyObject target)
        {
            foreach (DependencyProperty property in TranferProperties)
            {
                TransferProperty(source, target, property);
            }
        }

        static IEnumerable<DependencyProperty> TranferProperties
        {
            get
            {
                yield return DrawingElement.ThemeProperty;
                yield return DrawingElement.ColorSchemeProperty;
                yield return SpreadsheetElement.WorksheetProperty;
                yield return SpreadsheetElement.WorkbookProperty;
            }
        }
        static void TransferProperty(DependencyObject source, DependencyObject target, DependencyProperty property)
        {
            target.SetValue(property, source.GetValue(property));
        }
    }
}
